package com.loser.utils.trace;

import org.slf4j.MDC;
import org.springframework.util.StringUtils;

/**
 * 链路工具
 *
 * @author alan
 * @date 2022/9/5 18:44
 */
public class MDCUtil {

    public final static String TRACE_ID = "traceID";

    public static boolean setTraceId() {
        if (MDC.get(TRACE_ID) == null) {
            MDC.put(TRACE_ID, TraceUtil.getTraceId());
            return true;
        }
        return false;
    }

    public static boolean setTraceId(String traceId) {
        if (MDC.get(TRACE_ID) == null) {
            if (StringUtils.isEmpty(traceId)) {
                traceId = TraceUtil.getTraceId();
            }
            MDC.put(TRACE_ID, traceId);
            return true;
        }
        return false;
    }

    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    public static void removeTraceId() {
        MDC.remove(TRACE_ID);
    }
}
